using System;
using System.Collections.Generic;
using System.Text;

using PastCrew.Games.Chess.Engine.Entities;

namespace PastCrew.Games.Chess.Engine.Entities.Pieces
{
    public sealed class Bishop : BasePiece
    {
        #region constructors

        /// <summary>
        /// 
        /// </summary>
        public Bishop()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pieceColor"></param>
        public Bishop(Color pieceColor)
        {
            base.Color = pieceColor;
        }

        #endregion

        #region public methods

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public sealed override List<Entities.SquareStatus> GetValidMoves()
        {
            #region knight rule set

            Board.Position currentPosition = this.CurrentSquare.Position;
            List<Entities.SquareStatus> validPositions = new List<Entities.SquareStatus>();

            //  X         T       X     T
            //   X       X       X       X
            //    T     X       T         X
            //            

            // Bishop moves diagonally right/down
            if (currentPosition.X < 7 && currentPosition.Y > 0)
            {
                int y = (currentPosition.Y - 1);
                for (int x = (currentPosition.X + 1); x <= 7 && y >=0 ; x++, y--)
                {
                    Board.Square square = Board.GetSquare(x, y);
                    if (!square.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                        break;
                    }
                }
            }

            // Bishop moves diagonally right/up
            if (currentPosition.X < 7 && currentPosition.Y < 7)
            {
                int y = (currentPosition.Y + 1);
                for (int x = (currentPosition.X + 1); x <= 7 && y <= 7; x++, y++)
                {
                    Board.Square square = Board.GetSquare(x, y);
                    if (!square.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                        break;
                    }
                }
            }
            
            // Bishop moves diagonally left/down
            if (currentPosition.X > 0 && currentPosition.Y > 0)
            {
                int y = (currentPosition.Y - 1);
                for (int x = (currentPosition.X - 1); x >= 0 && y >= 0; x--, y--)
                {


                    Board.Square square = Board.GetSquare(x, y);
                    if (!square.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                        break;
                    }
                }
            }

            // Bishop moves diagonally left/up
            if (currentPosition.X > 0 && currentPosition.Y < 7)
            {
                int y = (currentPosition.Y + 1);
                for (int x = (currentPosition.X - 1); x >= 0 && y <= 7; x--, y++)
                {
                    Board.Square square = Board.GetSquare(x, y);
                    if (!square.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                        break;
                    }
                }
            }
            


            #endregion

            return validPositions;
        }

        #endregion
    }
}
